<?php
/**
 *   Joomdiet - administrator/components/models/foodcategories.php
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');
class BbitsdModelCountries extends JModel
{
    var $_data = array();
    var $_country; 
    var $_pagination;
    
    function __construct()
    {
        parent::__construct();
        
        global $mainframe, $option;
        
        try {
            $array = JRequest::getVar('cid', 0, '', 'array');
            $this->setId((int)$array[0]);
        } catch (Exception $e) {
            // do nothing
        }
        
        // loading pagination support
        $limit = $mainframe->getUserStateFromRequest('global.list.limit',
                                                     'limit',
                                                     $mainframe->getCfg('list_limit'),
                                                     'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
    }
    
    
    // default query constructor
    function _buildQuery() {
        $query = 'SELECT * FROM #__bbitsd_countries';
        return $query;
    }
    
    function setId($id)
    {
        $this->_id = $id;
        $this->_variable = null;
    }
    
    function getData()
    {
        if (empty($this->_data)) {
            $query = $this->_buildQuery();
            if (empty($this->_pagination)) {
                $this->_data = $this->_getList($query);
            } else {
                $this->_data = $this->_getList($query, 
                                               $this->getState('limitstart'),
                                               $this->getState('limit'));
            }
            if (empty($this->_data)) $this->_data = array();
        }
        
        return $this->_data;
    }
    
    
    
   	function getCountry1()
    {
        if ($this->_loadCountry()) {
        } else $this->_initCountry();
        
        return $this->_country;
    }
    
    function getCountryById($id) {
        $query = 'SELECT * FROM #__bbitsd_countries WHERE id='.$id;
        $this->_db->setQuery($query);
        return $this->_db->loadObject();
    }
    
    function _loadCountry()
    {
        if (empty($this->_country))
        {
            $query = 'SELECT * FROM #__bbitsd_countries WHERE id='.$this->_id;
            $this->_db->setQuery($query);
            $this->_country = $this->_db->loadObject();
            
            return (boolean) $this->_country;
        }
    }
    
    function _initCountry()
    {
        if (empty($this->_country)) {
            $country = new stdClass();
            $country->id = 0;
            $country->group_id = 0;
            $country->title = null;
            $country->flag = null;
            $country->description = null;

            
            $this->_country = $country;
            return (boolean) $this->_country;
        }
        
        return true;
    }
    

    function delete($id)
    {
            // deleting food
            $query = 'DELETE FROM #__bbitsd_countries WHERE id='.$id;
            $this->_db->setQuery($query);
            $this->_db->query();
            return true;
    }
    
     function store() 
    {
        $this->getCountry1();
        
        $this->_country->title = JRequest::getVar('country_title');
		$this->_country->group_id = JRequest::getVar('group_id');
        $this->_country->id = JRequest::getVar('country_id');
        $this->_country->description = JRequest::getVar('description', '', 'post', 'string', JREQUEST_ALLOWHTML);
        
        $this->_country->flag = JRequest::getVar('flag');
        
        if (!$this->_country->id) {
            $query = 'INSERT INTO #__bbitsd_countries'.
                ' (title,zone,flag,description) VALUES'.
                ' ("'.$this->_country->title.'","'.$this->_country->group_id.'","'.$this->_country->flag.'","'.mysql_escape_string($this->_country->description).'")';
        } else {
            $query = 'UPDATE #__bbitsd_countries '.
                ' SET title="'.$this->_country->title.'", zone="'.$this->_country->group_id.'", flag="'.$this->_country->flag.'", description="'.mysql_escape_string($this->_country->description).'"'.
                ' WHERE id='.$this->_country->id;
        }
        
        $this->_db->setQuery($query);
        $this->_db->query();
        
        if (!$this->_country->id) {
            return $this->_db->insertid();
        } else return $this->_country->id;
    }
    

    function getPagination()
    {
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(),
                                                 $this->getState('limitstart'),
                                                 $this->getState('limit'));
        }
        
        return $this->_pagination;
    }
    
        function getTotal()
    {
        if (empty($this->_total)) {
            $query = $this->_buildQuery();
            $this->_total = $this->_getListCount($query);
        }
        return $this->_total;
    }
        function getModel()
    {
        return $this;
    }
    
    
    function publishList($cid = array(), $publish = 1) {
         $user  =& JFactory::getUser();
         if (count( $cid ))
         {
             JArrayHelper::toInteger($cid);
             $cids = implode( ',', $cid );
             $query = 'UPDATE #__bbitsd_countries'
                   . ' SET state  = '.(int) $publish
                   . ' WHERE id IN ( '.$cids.' )'
                 . ' AND ( checked_out = 0 OR ( checked_out = '.(int)
$user->get('id').' )' ;
                  $this->_db->setQuery( $query );
                if (!$this->_db->query()) {
                    $this->setError($this->_db->getErrorMsg());
                    return false;
                 }
           }
           return true;
 	} 
    
    
   }
?>
